package com.hldsmart.smartda.activity.footprint;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.activity.mine.QuanDetailActivity;
import com.hldsmart.smartda.base.BaseActivity;
import com.hldsmart.smartda.commonutils.ImageLoadOptions;
import com.hldsmart.smartda.commonutils.widget.CustomDialog;
import com.nostra13.universalimageloader.core.ImageLoader;


import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.relex.circleindicator.CircleIndicator;

public class ShopDetailActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {


    @Bind(R.id.iv_shopdetail_back)
    ImageView iv_shopdetail_back;
    @Bind(R.id.iv_shopdetail_phone)
    ImageView iv_shopdetail_phone;

    @Bind(R.id.ll_shopDetail_quan)
    LinearLayout ll_shopDetail_quan;
    @Bind(R.id.ll_shopDetail_quan2)
    LinearLayout ll_shopDetail_quan2;

    private static final int PAGER_UPDATE = 0x000;
    /* 模拟广告栏跳转的uuid */
    private static final String UUID = "2308481E5A4546AFA627E023A7CC5D9F";
    @Bind(R.id.goods_item_viewpager)
    ViewPager viewPager;
    @Bind(R.id.goods_item_indicator)
    CircleIndicator indicator;
    private ArrayList<ImageView> list;
    private ArrayList<String> listarry;
    private MyHandler handler;
    private int count = 1;
//    private int[] banner_drawable = {R.drawable.img_dress, R.drawable.img_toys, R.drawable.img_book, R.drawable.img_household};


    public static void startShopDetailActivity(Context context) {
        Intent intent = new Intent(context, ShopDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_detail);
        ButterKnife.bind(this);
        setListener();
        initData();
        initView();
    }

    private void setListener() {
        iv_shopdetail_back.setOnClickListener(this);
        iv_shopdetail_phone.setOnClickListener(this);
        ll_shopDetail_quan.setOnClickListener(this);
        ll_shopDetail_quan2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_shopdetail_back:
                finish();
                break;
            case R.id.iv_shopdetail_phone:
                CustomDialog.Builder builder = new CustomDialog.Builder(this);
                builder.setTitle(R.string.prompt);
                builder.setMessage(R.string.kefu_phone);
                builder.setPositiveButton(R.string.call_phone, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_CALL);
                        Uri data = Uri.parse("tel:" + "15650701912");
                        intent.setData(data);
                        if (ActivityCompat.checkSelfPermission(ShopDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        startActivity(intent);
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                break;

            case R.id.ll_shopDetail_quan:
            case R.id.ll_shopDetail_quan2:
                QuanDetailActivity.startQuanDetailActivity(this);
                break;
        }
    }


    private void initData(){
        listarry=new ArrayList<>();
        listarry.add("http://img5.imgtn.bdimg.com/it/u=14633171,2677665342&fm=21&gp=0.jpg");
        listarry.add("http://img4.jiameng.com/2016/11/S6z97612L2SW.png");
        listarry.add("http://img0.imgtn.bdimg.com/it/u=1321516388,3413504012&fm=21&gp=0.jpg");
        listarry.add("http://img5.imgtn.bdimg.com/it/u=14633171,2677665342&fm=21&gp=0.jpg");

    }

    /*将图片路径转化为一个ImageView*/
    private void getImage() {
        list = new ArrayList<>();
        for (int i = 0; i < listarry.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageLoader.getInstance()
                    .displayImage(listarry.get(i),
                            imageView, ImageLoadOptions.build_item());
            list.add(imageView);
        }
    }


    private void initView() {

//        list = new ArrayList<>();



//        for (int i = 0; i < 4; i++) {
//            ImageView im = new ImageView(this);
//            im.setBackgroundResource(banner_drawable[i]);
//            im.setTag(im.getClass().getName() + i);
//            im.setOnClickListener(this);
//            list.add(im);
//        }

        getImage();
        viewPager.setAdapter(new ViewPagerAdapter());
        viewPager.addOnPageChangeListener(this);
        indicator.setViewPager(viewPager);
        handler = new MyHandler();
        handler.sendEmptyMessageDelayed(PAGER_UPDATE, 2000);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        count = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }


    public class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == PAGER_UPDATE) {
                if (count == list.size())
                    count = 0;
                viewPager.setCurrentItem(count++);
                sendEmptyMessageDelayed(PAGER_UPDATE, 2000);
            }

        }
    }
}








